﻿<%@ WebHandler Language="C#" Class="go" %>

using System;
using System.Web;
using Whir.Common;
using Whir.Common.WebHelper;

public class go : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string action = NetRequest.GetQueryString("Action");
        int columnID = NetRequest.GetQueryInt("columnID");

        if (!string.IsNullOrEmpty(action))
        {
            Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
            Whir.Model.Whir_Column model = new Whir.Model.Whir_Column();
            model = bll.GetModel(columnID);

            StartIt(context, action, model);
        }
    }

    /// <summary>
    /// 参数类别
    /// </summary>
    /// <param name="action"></param>
    /// <param name="model"></param>
    private void StartIt(HttpContext context, string action, Whir.Model.Whir_Column model)
    {
        switch (action)
        {
            case "PreviewItem":
                PreviewItem(context, model);
                break;
            case "EditModules":
                EditModules(context, model);
                break;
            case "Recyclebin":
                Recyclebin(context, model);
                break;
            case "ReNameColumn":
                string editItemvalue = NetRequest.GetQueryString("editItemvalue");
                ReNameColumn(context, model, editItemvalue);
                break;
            case "DeleteColumn":
                DeleteColumn(context, model);
                break;
            case "AddChildColumn":
                AddChildColumn(context);
                break;
        }
    }

    /// <summary>
    /// 添加子栏目
    /// </summary>
    private void AddChildColumn(HttpContext context)
    {
        int parentID = NetRequest.GetQueryInt("parentID");
        string childItemvalue = NetRequest.GetQueryString("ChildItemvalue");

        Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
        Whir.Model.Whir_Column model = new Whir.Model.Whir_Column();

        model.columnMark = "Whir_" + DateTime.Now.ToString();
        model.moduleMark = "-1";
        model.modelID = -1;
        model.formID = "-1";

        model.parentID = parentID;
        model.columnName = childItemvalue;
        model.OperatePage = "";
        model.param = "";
        model.sort = 0;
        model.url = "";
        model.pic = "";
        model.metaTitle = "";
        model.metaKey = "";
        model.metaDesc = "";
        model.listTemp = "";
        model.defaultTemp = "";
        model.contentTemp = "";
        model.isCreateHtml = true;
        model.isUse = true;

        model.createDate = DateTime.Now;
        model.createUserID = RoleHelper.GetAdminID();
        model.updateDate = model.createDate;
        model.updateUserID = model.createUserID;
        model.directory = "";
        model.IsSinglePage = false;
        new Whir.SQLServerDAL.Whir_Column().Add(model);

        //同步权限
        RoleHelper.AddDefaultRole();
        RoleHelper.AddCacheRole();
        //end
    }

    /// <summary>
    /// 删除栏目
    /// </summary>
    private void DeleteColumn(HttpContext context, Whir.Model.Whir_Column model)
    {
        Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
        bll.Delete(model.columnID);
    }

    /// <summary>
    /// 预览
    /// </summary>
    private void ReNameColumn(HttpContext context, Whir.Model.Whir_Column model, string newname)
    {
        Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
        model.columnName = newname;
        bll.Update(model);
    }


    /// <summary>
    /// 预览发布的页面
    /// </summary>
    /// <param name="model"></param>
    private void PreviewItem(HttpContext context, Whir.Model.Whir_Column model)
    {
        string url = "";
        //首页
        if (!string.IsNullOrEmpty(model.defaultTemp))
        {
            url = model.directory + "/" + model.url;
            context.Response.Redirect("~/" + url);
        }
        else if (model.IsSinglePage)//单篇信息
        {
            url = model.directory + "/" + model.url;
            context.Response.Redirect("~/" + url);
        }
        else if (!model.IsSinglePage && string.IsNullOrEmpty(model.defaultTemp))//列表信息// 
        {
            url = model.directory + "/" + model.url; //url = model.directory + "/" + model.url.Split('.')[0] + "_list.html";
            context.Response.Redirect("~/" + url);
        }
        else if (!model.IsSinglePage && !string.IsNullOrEmpty(model.listTemp))//列表信息// 
        {
            url = model.directory + "/" + model.url.Split('.')[0] + "_list.html";
            context.Response.Redirect("~/" + url);
        }
        else if (!model.IsSinglePage && string.IsNullOrEmpty(model.listTemp) && !string.IsNullOrEmpty(model.contentTemp))//内容页 
        {
            url = model.directory + "/" + model.url;
            context.Response.Redirect("~/" + url);
        }
        else
        {
            context.Response.Write("<script language=javascript>alert(\"该栏目没有静态页面\");window.opener=null;window.open(\"\",\"_self\");window.close();</script>");
        }
    }

    /// <summary>
    /// 更新当前栏目模板
    /// </summary>
    /// <param name="model"></param>
    private void EditModules(HttpContext context, Whir.Model.Whir_Column model)
    {
        if (!string.IsNullOrEmpty(model.defaultTemp))
        {
            string url = "";
            url = model.defaultTemp;
            string syspath = Settings.GetString("SystemPath");
            //url = url.Replace(syspath, "~/");
            url = context.Server.MapPath(url);
            url = context.Server.UrlEncode(url);
            context.Response.Redirect("~/Whir_System/Module/Template/TemplateLabel.aspx?dir=" + url);
        }
        else if (model.IsSinglePage)//单篇信息
        {
            string url = "";
            url = model.contentTemp;
            string syspath = Settings.GetString("SystemPath");
            //url = url.Replace(syspath, "~/");
            url = context.Server.MapPath(url);
            url = context.Server.UrlEncode(url);
            context.Response.Redirect("~/Whir_System/Module/Template/TemplateLabel.aspx?dir=" + url);
        }
        else if (!model.IsSinglePage && (!string.IsNullOrEmpty(model.listTemp) || !string.IsNullOrEmpty(model.contentTemp)))//列表信息
        {
            string url = "";
            url = model.listTemp;
            string syspath = Settings.GetString("SystemPath");
            //url = url.Replace(syspath, "~/");
            url = context.Server.MapPath(url);
            url = context.Server.UrlEncode(url);
            context.Response.Redirect("~/Whir_System/Module/Template/TemplateLabel.aspx?dir=" + url);
        }
        else
        {
            context.Response.Write("<script language=javascript>alert(\"该栏目没有绑定模板\");window.opener=null;window.open(\"\",\"_self\");window.close();</script>");
        }
    }

    /// <summary>
    /// 回收站
    /// </summary>
    /// <param name="context"></param>
    /// <param name="model"></param>
    private void Recyclebin(HttpContext context, Whir.Model.Whir_Column model)
    {
        string str = "";
        if (model.modelID == -1 || model.formID == "-1")
        {
            str = "Failed";
        }
        else
        {
            str = "Module/CommonContents/RecycleBinContentManage.aspx?ModelID={0}&ModelFormID={1}&TypeID={2}&IsRecycle=1";
        }
        context.Response.Write(string.Format(str, model.modelID, model.formID, model.columnID));
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}
